package com.smartao.base.selecttype;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.smartao.base.module.Module;
import com.smartao.base.module.ModuleService;
import com.smartao.base.selectitem.Selectitem;
import com.smartao.base.selectitem.SelectitemService;
import com.smartao.core.mvc.BaseAction;
import com.smartao.core.mvc.ResultStatus;

@Controller
@Scope("prototype")
@Results({ @Result(name = "success", location="list.jsp"),
	@Result(name = "input", location="input.jsp"),
	@Result(name = "view", location="view.jsp"),
	@Result(name = "listForUpdate", location="listForUpdate.jsp")
})
public class SelecttypeAction extends BaseAction{

	private static final long serialVersionUID = -8044873461195869539L;

	@Autowired
	private SelecttypeService selecttypeService;
	
	@Autowired
	private SelectitemService selectitemService;
	
	@Autowired
	private ModuleService moduleService;
	
	private Module module;
	
	private Selectitem selectitem;
	
	private List<Selecttype> selecttypes;
	
	private Selecttype selecttype;
	
	private Long moduleId;
	
	@Override
	public String execute(){
		return SUCCESS;
	}
	
	public String listForUpdate(){
		
		selecttypes = selecttypeService.findByModule(moduleId);

		if(selecttypes!=null&&selecttypes.size()!=0){
			module = selecttypes.get(0).getModule();
		}
		else{
			module = moduleService.findOne(moduleId);
		}
		return "listForUpdate";
	}
	
	public String update(){
		if(selecttypes!=null){
			for(Selecttype tp : selecttypes){
				if(tp!=null){
					moduleId = tp.getModule().getId();
					selecttypeService.save(tp);
				}
			}
		}
		resultStatus = ResultStatus.SUCCESS;
		return listForUpdate();
	}
	
	@Override
	public String input(){
		if(id==null){
			selecttype = new Selecttype();
			module = moduleService.findOne(moduleId);
			selecttype.setModule(module);
		}
		else{
			selecttype = selecttypeService.findOne(id);
			module = selecttype.getModule();
		}
		return INPUT;
	}
	
	public String delete(){
		// 确保moduleId有值，在listForUpdate中要用到；
		if(moduleId==null){
			selecttype = selecttypeService.findOne(id);
			moduleId = selecttype.getModule().getId();
		}
		
		long count = selectitemService.countBySelecttype(id);
		if(count>0){
			resultStatus = new ResultStatus(false, "请先删除下属选择项.");
		}
		// TODO: 删除选择项类型还需做哪些考虑？
		else{
			selecttypeService.delete(id);
			resultStatus = ResultStatus.SUCCESS;
		}
		return listForUpdate();
	}

	public Module getModule() {
		return module;
	}

	public void setModule(Module module) {
		this.module = module;
	}

	public Selectitem getSelectitem() {
		return selectitem;
	}

	public void setSelectitem(Selectitem selectitem) {
		this.selectitem = selectitem;
	}

	public List<Selecttype> getSelecttypes() {
		return selecttypes;
	}

	public void setSelecttypes(List<Selecttype> selecttypes) {
		this.selecttypes = selecttypes;
	}

	public Selecttype getSelecttype() {
		return selecttype;
	}

	public void setSelecttype(Selecttype selecttype) {
		this.selecttype = selecttype;
	}

	public Long getModuleId() {
		return moduleId;
	}

	public void setModuleId(Long moduleId) {
		this.moduleId = moduleId;
	}
	
}
